/**
 * 系统项目名称
 * com.ytbh.filter
 * LoginFilter.java
 * 
 *  2016年9月25日-下午7:19:50
 *  2016SUN公司-版权所有
 *
 */
package com.duk.filter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 
* @author  by: kasher
* @date 创建时间：2016年9月25日 下午7:19:50 
* @version 1.0 * @parameter  
* @since  
* @return  
*/
/**
 *
 * LoginFilter by:kasher 2016年9月25日 下午7:19:50 我乃压身第一人
 * 
 * @version 1.0.0
 *
 */
public class LoginFilter implements Filter
{
	// 不受检查的资源
	private List<String> unCheckUrl = null;

	private String loginPage = "/login/login.do";

	private String userInSession = "user_in_session";

	public void init(FilterConfig config) throws ServletException
	{
		System.out.println("filter初始化");
		// 获取不用拦截的路径地址用‘，’分割，然后转成list
		this.unCheckUrl = Arrays.asList(config.getInitParameter("unCheckUrl").split(","));
		// 获取设置没有登录用户所到的页面
		this.loginPage = config.getInitParameter("loginPage");
		// 获取登录用户的session
		this.userInSession = config.getInitParameter("userInSession");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException
	{
		// 把参数都转成带http的才支持

		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String requestUri = req.getRequestURI();
		String list = unCheckUrl.toString();
		System.out.println("requestUri=" + requestUri);
		System.out.println("list=" + list);
		/*
		 * if ("/ytbh/login/login.do".equals(requestUri) |
		 * "/ytbh/WEB-INF/views/admin/Public/login.jsp".equals(requestUri)) {
		 * chain.doFilter(req, res); }
		 */
		if (!list.contains(requestUri))
		{
			Object userSession = req.getSession().getAttribute(userInSession);
			if (userSession == null)
			{
				res.sendRedirect(req.getContextPath() + loginPage);
				return;
			}
		}
		chain.doFilter(req, res);
	}

	public void destroy()
	{

	}

}
